#!/usr/bin/perl

sub CTCiUsage {
    print STDERR "Usage: ctci [-c \"comment\"] [-nc] element-name [...]\n" ;
    print STDERR "Options:\n" ;
    print STDERR "  -c \"comment\" : provide a comment about this action\n" ;
    print STDERR "  -nc : expect no comment on this action\n" ;
    exit;
}

if ( $#ARGV < 0 ) {
    &CTCiUsage ;
}

$tool = $ENV{"DTOOL"} ;
if ( $tool eq "" ) {
   die "Environment not configured for CTtools" ;
}

require "$tool/built/include/ctutils.pl" ;
require "$tool/built/include/ctvspec.pl" ;
require "$tool/built/include/ctquery.pl" ;
require "$tool/built/include/ctproj.pl" ;
require "$tool/built/include/ctcm.pl" ;

$comment = "" ;

$skip = 0 ;

@files = () ;

foreach $item ( @ARGV ) {
  if ( $skip == 0 ) {
      if ( $item eq "-nc" ) {
          &CTUDebug( "-nc processed\n" ) ;
      } elsif ( $item eq "-c" ) {
          $skip = 1 ;
      } else {
          push( @files, $item ) ;
          &CTUDebug( "added '" . $item . "' to files to be processed\n" ) ;
      }
  } elsif ( $skip == 1 ) {
      $comment = $item ;
      &CTUDebug( "setting comment to '" . $comment . "'\n" ) ;
      $skip = 0 ;
  } else {
      &CTUDebug( "got to unknown skip value! (" . $skip . ")\n" ) ;
      $skip = 0 ;
  }
}


if ($#files < 0 ) {
    &CTCiUsage ;
}

$projname = &CTProj ;
$projname =~ tr/A-Z/a-z/ ;
$flav = &CTQueryProj( $projname ) ;
$spec = &CTResolveSpec( $projname, $flav ) ;

foreach $item ( @files ) {
    if ( -e $item ) {
	if ( ! &CTCMCheckin( $item, $projname, $spec, $comment ) ) {
	    print STDERR "Could not checkin '$item'\n" ;
	}
    } else {
	print STDERR "No such file '$item'.\n" ;
    }
}
